<?php

namespace app\admin\model\xmwms;

use think\Model;

/**
 * InventoryHistory
 */
class InventoryHistory extends Model
{
    // 表名
    protected $name = 'wms_inventory_history';

    /**
     * @var string 自动写入时间戳字段
     */
    protected $autoWriteTimestamp = 'true';

    protected $append = [
        'form_type_text',
        'warehouse_shelf_id_text',
    ];

    // 字段类型转换
    protected $type = [
        'deletetime' => 'timestamp:Y-m-d H:i:s',
    ];

    /**
     * @param        $type
     * @param string $change into 入库   out 出库   move移库
     * @return string
     */
    public function typeToFormType($type, $change)
    {
        if ($change == 'into') {
            switch ($type) {
                case 10:
                    $value = 10;
                    break;
                case 20:
                    $value = 20;
                    break;
                case 30:
                    $value = 30;
                    break;
                default:
                    $value = '';
            }
        } elseif ($change == 'out') {
            switch ($type) {
                case 10:
                    $value = 40;
                    break;
                case 20:
                    $value = 50;
                    break;
                case 30:
                    $value = 60;
                    break;
                default:
                    $value = '';
            }
        } else {
            $value = 70;
        }
        return $value;
    }

    //类型：10=采购入库；20=外协入库；30=退货入库；40=销售出库；50=外协出库；60=调拨入库；70=库存移动
    public function getFormTypeTextAttr($value, $row): string
    {
        if ($row['form_type'] === '' || $row['form_type'] === null) return '';
        switch ($row['form_type']) {
            case 10:
                $value = '采购入库';
                break;
            case 20:
                $value = '外协入库';
                break;
            case 30:
                $value = '退货入库';
                break;
            case 40:
                $value = '销售出库';
                break;
            case 50:
                $value = '外协出库';
                break;
            case 60:
                $value = '调拨入库';
                break;
            case 70:
                $value = '库存移动';
                break;
            default:
                $value = '';
        }
        return $value;
    }

    public function getWarehouseShelfIdTextAttr($value, $row): string
    {
        if (isset($row['warehouse_shelf_id']) && $row['warehouse_shelf_id'] > 0) {
            $model = new Warehouse();
            $data  = $model->where('id', $row['warehouse_shelf_id'])->find();
            if ($data['warehouse_type'] == 'warehouse_shelf') {
                $warehouse_area = $model->where('id', $data['pid'])->find();
                $warehouse      = $model->where('id', $warehouse_area['pid'])->value('name');
                return $warehouse . '/' . $warehouse_area['name'] . '/' . $data['name'];
            } elseif ($data['warehouse_type'] == 'warehouse_area') {
                $warehouse = $model->where('id', $data['pid'])->value('name');
                return $warehouse . '/' . $data['name'];
            } else {
                return $data['name'];
            }
        } else {
            return '';
        }
    }

    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id', 'id');
    }

    public function admin()
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
    }

}